<?php
namespace app\admin\validate;

/**
 * 验证器
 */
use think\Validate;

class Arctype extends Validate
{
    protected $rule = [
        'title' => 'require|max:25|unique:arctype',
        'pid'   => 'number',
    ];

    protected $message = [
        'title.require' => '名称必须',
        'title.max'     => '名称最多不能超过25个字符',

        'pid.number'    => '上级栏目参数错误',
        'pid.checkpid'  => '上级栏目不能为自身',

    ];
    // edit 验证场景定义
    public function sceneEdit()
    {
        return $this->only(['title', 'pid'])
            ->remove('title', 'unique')
            ->append('title', 'unique:arctype')
            ->append('pid', 'checkpid:pid');

    }

    // 自定义验证规则
    protected function checkpid($value, $rule, $data = [])
    {
        if ($data['id'] === $data['pid']) {
            return false;
        } else {
            return true;
        }

    }
}
